package auths

import (
	"errors"
	"github.com/dgrijalva/jwt-go"
)

type RbacClaim struct {
	jwt.StandardClaims
	UserInfo
}

func ParseRbac(publicKey []byte, tokenValue string) (*RbacClaim, error) {
	token, err := jwt.ParseWithClaims(tokenValue, &RbacClaim{}, func(token *jwt.Token) (interface{}, error) {
		return jwt.ParseRSAPublicKeyFromPEM(publicKey)
	})
	if err != nil {
		return nil, err
	}
	if token == nil || !token.Valid {
		return nil, errors.New("解析jwt失败")
	}
	if claims, ok := token.Claims.(*RbacClaim); ok {
		return claims, nil
	} else {
		return nil, errors.New("解析jwt失败")
	}

}
